package com.liuxl.core.service;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuxl.core.common.base.BaseService;
import com.liuxl.core.common.support.Convert;
import com.liuxl.core.mapper.auto.OrganDicMapper;
import com.liuxl.core.mapper.auto.TsysUserMapper;
import com.liuxl.core.model.auto.OrganDic;
import com.liuxl.core.model.auto.OrganDicExample;
import com.liuxl.core.model.auto.TSysDictData;
import com.liuxl.core.model.auto.TSysDictDataExample;
import com.liuxl.core.model.auto.TsysPermission;
import com.liuxl.core.model.auto.TsysPermissionExample;
import com.liuxl.core.model.auto.TsysUser;
import com.liuxl.core.model.custom.BootstrapTree;
import com.liuxl.core.model.custom.Tablepar;
import com.liuxl.core.util.SnowflakeIdWorker;
import com.liuxl.core.util.StringUtils;

/**
 * 机构管理 OrganDicService
 * @Title: OrganDicService.java 
 * @Package com.liuxl.core.service 
 * @author liuxl_自动生成
 * @email liuxl@gisspring.com
 * @date 2019-11-14 17:23:04  
 **/
@Service
public class OrganDicService implements BaseService<OrganDic, OrganDicExample>{
	@Autowired
	private OrganDicMapper organDicMapper;
	
	@Autowired
	private TsysUserMapper tsysUserMapper;
      	   	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<OrganDic> list(Tablepar tablepar,String name){
	        OrganDicExample testExample=new OrganDicExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andTNameLike("%"+name+"%");
	        }

	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<OrganDic> list= organDicMapper.selectByExample(testExample);
	        PageInfo<OrganDic> pageInfo = new PageInfo<OrganDic>(list);
	        return  pageInfo;
	 }
	 
	 public List<OrganDic> list2(String searchTxt){
		    OrganDicExample testExample=new OrganDicExample();
	        testExample.setOrderByClause("id+0 asc");
	        if(searchTxt!=null&&!"".equals(searchTxt)){
	        	testExample.createCriteria().andTNameLike("%"+searchTxt+"%");
	        }
	        List<OrganDic> list= organDicMapper.selectByExample(testExample);
	        return  list;
		 }

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=Convert.toListStrArray(ids);
			OrganDicExample example=new OrganDicExample();
			example.createCriteria().andIdIn(lista);
			return organDicMapper.deleteByExample(example);
			
				
	}
	
	
	/**
	 * 获取转换成bootstarp的权限数据
	 * @return
	 */
	public BootstrapTree getbooBootstrapTreePerm(String userid){
		List<BootstrapTree> treeList = new ArrayList<BootstrapTree>();
		List<OrganDic> menuList =  getall(userid);
		treeList = getbooBootstrapTreePerm(menuList,"0");
		if(treeList!=null&&treeList.size()==1) {
			return treeList.get(0);
		}
		return new BootstrapTree("菜单", "fa fa-home", "", "-1","###",0,treeList,"",0);
	}
	
	/**
	 * 获取树
	 * @param menuList
	 * @param parentId
	 * @return
	 */
	private static List<BootstrapTree> getbooBootstrapTreePerm(List<OrganDic> menuList, String parentId){
		List<BootstrapTree> treeList = new ArrayList<>();
		List<BootstrapTree> childList = null;
		for(OrganDic p : menuList) {
			p.setPid(p.getPid()==null||p.getPid().trim().equals("")?"0":p.getPid());
			if(p.getPid().trim().equals(parentId)) {
				if(p.getChildcount()!=null&&p.getChildcount()>0) {
					childList = getbooBootstrapTreePerm(menuList, String.valueOf(p.getId()));
				}
				BootstrapTree bootstrapTree = new BootstrapTree(p.getTName(), "", "", String.valueOf(p.getId()),"",null, childList,"",Integer.parseInt(p.getStatus()));
				treeList.add(bootstrapTree);
				childList = null;
			}
		}
		return treeList.size() >0 ? treeList : null;
	}
	
	/**
	 * 根据用户id获取用户角色如果用户为null 获取所有权限
	 * @return
	 */
	public List<OrganDic> getall(String userid){
//		if(StringUtils.isEmpty(userid)) {
		OrganDicExample example = new OrganDicExample();
			//example.setOrderByClause("order_num asc");
			return  organDicMapper.selectByExample(example);
//		}
	}
	
	@Override
	public OrganDic selectByPrimaryKey(String id) {
				
			return organDicMapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	@Transactional
	public int updateByPrimaryKeySelective(OrganDic record) {
		for(String id: record.getUserIds().split(",")) {
			TsysUser user = new TsysUser();
			user.setId(id);
			user.setOrgan_id(record.getId());
			System.out.println("record.getTName()::"+record.getTName());
			user.setOrgan_name(record.getTName());
			tsysUserMapper.updateByPrimaryKeySelective(user);
		}
		
		return organDicMapper.updateByPrimaryKeySelective(record);
	}
	
	
	public List<OrganDic> getDictbyUser(String pid) {
		OrganDicExample example = new OrganDicExample();
		example.createCriteria().andPidEqualTo(pid);
		return organDicMapper.selectDictByUser(example);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	@Transactional
	public int insertSelective(OrganDic record) {
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		for(String id: record.getUserIds().split(",")) {
			TsysUser user = new TsysUser();
			user.setId(id);
			user.setOrgan_id(record.getId());
			user.setOrgan_name(record.getTName());
			tsysUserMapper.updateByPrimaryKeySelective(user);
		}
		return organDicMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(OrganDic record, OrganDicExample example) {
		
		return organDicMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(OrganDic record, OrganDicExample example) {
		
		return organDicMapper.updateByExample(record, example);
	}

	@Override
	public List<OrganDic> selectByExample(OrganDicExample example) {
		
		return organDicMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(OrganDicExample example) {
		
		return organDicMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(OrganDicExample example) {
		
		return organDicMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param organDic
	 * @return
	 */
	public int checkNameUnique(OrganDic organDic){
		OrganDicExample example=new OrganDicExample();
		example.createCriteria().andTNameEqualTo(organDic.getTName());
		List<OrganDic> list=organDicMapper.selectByExample(example);
		return list.size();
	}

	
	

}
